﻿/*
' DotNetNuke® - http://www.dotnetnuke.com
' Copyright (c) 2002-2009
' by DotNetNuke Corporation
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
*/

using WatiN.Core;
using DotNetNuke.Tests.UI.WatiN.Common;

namespace DotNetNuke.Tests.UI.WatiN.Common.WatiNObjects
{
    public class HTMLModule : ModulePage
    {
        private TelerikEditor telerikEditor;

        #region Constructors

        public HTMLModule(WatiNBase watinBase) : base(watinBase.IEInstance, watinBase.SiteUrl, watinBase.DBName)
        {
            telerikEditor = new TelerikEditor(watinBase);
        }

        #endregion

        #region Public Properties

        #region TelerikEditor
        public TelerikEditor TelerikEditor
        {
            get { return telerikEditor; }
        }
        #endregion

        #region Links
        public Link ManageWorkFlowsLink
        {
            get { return ContentDiv.Link(Find.ByTitle("Manage Workflows")); }
        }
        public Link PreviewContentLink
        {
            get { return ContentDiv.Link(Find.ByTitle("Preview")); }
        }       
        public Link SaveVersionHistoryLink
        {
            get { return ContentDiv.Link(Find.ById(s => s.EndsWith("Workflow_cmdSaveVersions"))); }
        }
        #endregion

        #region Tables
        public Table PreviewTable
        {
            get { return ContentDiv.Table(Find.ById(s => s.EndsWith("EditHTML_tblPreview"))); }
        }
        public Table VersionTable
        {
            get { return ContentDiv.Table(Find.ById(s => s.EndsWith("EditHTML_grdVersions"))); }
        }
        #endregion

        #region Spans
        public Span DocumentManagerSpan
        {
            get { return ContentPane.Span(Find.ByClass("DocumentManager")); }
        }
        public Span PreviewSpan
        {
            get { return PreviewTable.Span(Find.ById(s => s.EndsWith("EditHTML_lblPreview"))); }
        }
        #endregion

        #region SelectLists
        public SelectList WorkflowSelectList
        {
            get { return IEInstance.SelectList(Find.ById(s => s.EndsWith("ModuleSettings_Settings_cboWorkflow"))); }
        }
        #endregion

        #region Images
        public Image VersionHistorySettingsExpandButton
        {
            get { return ContentDiv.Image(Find.ById(s => s.EndsWith("Workflow_dshVersions_imgIcon"))); }
        }
        public Image VersionHistoryTableExpandButton
        {
            get { return ContentDiv.Image(Find.ById(s => s.EndsWith("EditHTML_dshVersions_imgIcon"))); }
        }
        #endregion

        #region TextFields
        public TextField VersionHistoryTextField
        {
            get { return ContentDiv.TextField(Find.ById(s => s.EndsWith("Workflow_txtMaximumVersionHistory"))); }
        }

        #endregion

        #region CheckBoxes
        public CheckBox PublishCheckBox
        {
            get { return ContentDiv.CheckBox(Find.ById(s => s.EndsWith("EditHTML_chkPublish"))); }
        }
        #endregion

        #region Divs
        public Div HTMLContentDiv
        {
            get { return ContentPane.Div(Find.ById(s => s.EndsWith("HtmlModule_lblContent"))); }
        }
        #endregion

        #endregion

        #region Public Methods
        public void AddContentToModulePublishModule(string content, int moduleNum)
        {
            GetEditModuleContentLink(moduleNum).Click();
            SelectTextEditorStyleByName(IEInstance, "BASIC");
            ModuleTextBox.Value = content;
            PublishCheckBox.Checked = true;
            SaveContentLink.Click();
        }

        public TableCell GetStateCellByNumber(int versionNum)
        {
            //Gets the table cell that contains the versions state specified by the version number
            TableCell result = null;
            foreach (TableRow row in VersionTable.TableRows)
            {
                if (row.TableCells[2].InnerHtml.Contains(versionNum.ToString()))
                {
                    result = row.TableCells[5];
                    break;
                }
                continue;
            }
            return result;
        }

        public Image GetVersionPreviewButtonByNumber(int versionNum)
        {
            //Returns the Preview Button for the version specified 
            Image result = null;
            foreach (TableRow row in VersionTable.TableRows)
            {
                if (row.TableCells[2].InnerHtml.Contains(versionNum.ToString()))
                {
                    result = row.Image(Find.ByTitle("Preview Content"));
                    break;
                }
                continue;
            }
            return result;
        }

        public Image GetVersionRollbackButtonByNumber(int versionNum)
        {
            //Returns the Rollback Button for the version specified 
            Image result = null;
            foreach (TableRow row in VersionTable.TableRows)
            {
                if (row.TableCells[2].InnerHtml.Contains(versionNum.ToString()))
                {
                    result = row.Image(Find.ByTitle("Rollback Content To This Version"));
                    break;
                }
                continue;
            }
            return result;
        }

        #endregion
    }
}

